package com.greateffect.littlebud.lib.utils;

import java.math.BigDecimal;

/**
 * Created by ZengCS on 2019/5/21.
 * E-mail:zengcs@vip.qq.com
 * Add:成都市天府软件园E3
 */
public class DownloadUtils {
    /**
     * 转换速度
     */
    public static String parseSpeed(float speed) {
        String speedStr;
        // 如果大于922Kb/s 则显示单位为Mb/s
        if (speed < 922) {
            speedStr = new BigDecimal(speed).setScale(2, BigDecimal.ROUND_HALF_UP) + " Kb/s";
        } else {
            speed = speed / 1024;
            speedStr = new BigDecimal(speed).setScale(2, BigDecimal.ROUND_HALF_UP) + " Mb/s";
        }
        return speedStr;
    }

    /**
     * 转换百分比
     */
    public static String parseProgress(int soFarBytes, int totalBytes) {
        float progress = (float) soFarBytes / (float) totalBytes * 100;
        return new BigDecimal(progress).setScale(2, BigDecimal.ROUND_HALF_UP) + "%";
    }
}
